package animation.sort.algorithms;

import animation.sort.SortAnimation;
import animation.core.AlgorithmMonitorModel;
import animation.core.Monitoreable;
import animation.core.MonitorModelImpl;


public class BubbleSort
        extends SortAnimation implements Monitoreable
{

    public String toString()
    {
        return "Bubble Sort";
    }

    protected void sort(int a[])
            throws Exception
    {
        monitorModel.registerInvariant("N", a.length);
        for (int i = a.length - 1; i > 0; i--) {
            monitorModel.setVariableValue("i", i);
            boolean flipped = false;
            markBox(0, i);
            for (int j = 0; j < i; j++) {
                monitorModel.setVariableValue("j", j);
                if (greater(a, j, j + 1)) {
                    swap(a, j, j + 1);
                    flipped = true;
                }

            }
            if (!flipped) {
                return;
            }
        }
    }


    protected void registerSpecificVariables(AlgorithmMonitorModel monitorModel) {
        monitorModel.registerVariable("i", String.valueOf(0));
        monitorModel.registerVariable("j", String.valueOf(0));
    }


}
